package com.mogul.lahub.fragment

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hjq.shape.layout.ShapeLinearLayout
import com.hjq.shape.layout.ShapeRelativeLayout
import com.mogul.lahub.DensityUtil
import com.mogul.lahub.MainActivity
import com.mogul.lahub.MyItemDecoration
import com.mogul.lahub.R
import com.mogul.lahub.databinding.FragmentWithdruhneawCashBinding

class WithdrawCashFragment : Fragment() {

    private lateinit var withdruhneawCashBinding: FragmentWithdruhneawCashBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        withdruhneawCashBinding =
            FragmentWithdruhneawCashBinding.inflate(inflater, container, false)
        return withdruhneawCashBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val mainActivity = context as MainActivity
        withdruhneawCashBinding.rvAmoundnwjndwt.layoutManager = GridLayoutManager(mainActivity, 3)
        withdruhneawCashBinding.rvAmoundnwjndwt.addItemDecoration(
            MyItemDecoration(
                DensityUtil.dip2px(
                    mainActivity,
                    23f
                ), DensityUtil.dip2px(
                    mainActivity,
                    13f
                )
            )

        )
        withdruhneawCashBinding.rvAmoundnwjndwt.adapter =
            LoanAmountAdapter(mainActivity)

        withdruhneawCashBinding.rvTenuwjndwkre.layoutManager = GridLayoutManager(mainActivity, 3)
        withdruhneawCashBinding.rvTenuwjndwkre.addItemDecoration(
            MyItemDecoration(
                DensityUtil.dip2px(
                    mainActivity,
                    23f
                ), 0
            )

        )
        withdruhneawCashBinding.rvTenuwjndwkre.adapter =
            LoanTenureAdapter(mainActivity)
    }

    class LoanAmountAdapter(mContext: Context) :
        RecyclerView.Adapter<LoanAmountAdapter.MyViewHolder>() {

        private var selectPosition = 0
        private var arrayList = arrayListOf("5000", "6000", "7000", "8000", "10000", "15000")

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
            val view =
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_loan_amount_tx, parent, false)
            return MyViewHolder(view)
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            val mTvAmount = holder.mItemLlAmount.get(0) as TextView
            val mIvLock = holder.mItemLlAmount.get(1) as ImageView
            mTvAmount.setText(arrayList[position])
            if (selectPosition == position) {
                holder.mItemLlAmount.shapeDrawableBuilder
                    .setSolidColor(-0x3af6).intoBackground()
                mTvAmount.setTextColor(Color.WHITE)
            } else {
                holder.mItemLlAmount.shapeDrawableBuilder
                    .setSolidColor(Color.WHITE).intoBackground()
                mTvAmount.setTextColor(Color.BLACK)
            }
            holder.mItemLlAmount.setOnClickListener {
                if (selectPosition != holder.adapterPosition) {
                    selectPosition = holder.adapterPosition
                    notifyDataSetChanged()
                }
            }
        }

        override fun getItemCount(): Int {
            return arrayList.size
        }

        class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var mItemLlAmount: ShapeLinearLayout

            init {
                mItemLlAmount = itemView as ShapeLinearLayout
            }
        }
    }

    class LoanTenureAdapter(mContext: Context) :
        RecyclerView.Adapter<LoanTenureAdapter.TenureViewHolder>() {

        private var selectPosition = 0
        private var arrayList = arrayListOf("92", "120", "180")

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TenureViewHolder {
            val view =
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_loan_tenure_tx, parent, false)
            return TenureViewHolder(view)
        }

        override fun onBindViewHolder(holder: TenureViewHolder, position: Int) {
            val mTvTenure = holder.mItemRlTenure.get(0) as TextView
            val mIvLock = holder.mItemRlTenure.get(1) as ImageView
            mTvTenure.setText(arrayList[position])
            if (selectPosition == position) {
                holder.mItemRlTenure.shapeDrawableBuilder
                    .setSolidColor(-0x3af6).intoBackground()
                mTvTenure.setTextColor(Color.WHITE)
            } else {
                holder.mItemRlTenure.shapeDrawableBuilder
                    .setSolidColor(Color.WHITE).intoBackground()
                mTvTenure.setTextColor(Color.BLACK)
            }
            holder.mItemRlTenure.setOnClickListener {
                if (selectPosition != holder.adapterPosition) {
                    selectPosition = holder.adapterPosition
                    notifyDataSetChanged()
                }
            }
        }

        override fun getItemCount(): Int {
            return arrayList.size
        }

        class TenureViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var mItemRlTenure: ShapeRelativeLayout

            init {
                mItemRlTenure = itemView as ShapeRelativeLayout
            }
        }
    }
}